package com.ming.closeto;


import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


import com.ming.closeto.entity.Constant;
import com.ming.closeto.fragment.AppFragment;
import com.ming.closeto.fragment.HomeFragment;
import com.ming.closeto.fragment.SystemFragment;
import com.ming.closeto.fragment.TimerFragment;
import com.ming.closeto.fragment.WallpaperFragment;
import com.ming.closeto.ui.SettingActivity;
import com.ming.closeto.utils.LogUtils;

//9-3
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    private Toolbar mToolBar;
    private NavigationView main_nav_view;
    private DrawerLayout main_drawer_layout;

    private HomeFragment homeFragment;
    private WallpaperFragment wallpaperFragment;
    private AppFragment appFragment;
    private SystemFragment systemFragment;
    private TimerFragment timerFragment;

    private ButteryReceiver butteryReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         * application :
         * 1. 生命周期最长 初始化动作 每一个进程都会创建
         * 2. 应用默认有一个空的application
         * 3. name
         * 4. PID: android.os.Process.myPid();
         */
        initToolBar();
        initView();
        initHeaderView();
        initReceiver();
    }

    private void initReceiver() {
        butteryReceiver = new ButteryReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(butteryReceiver, intentFilter);
    }

    private void initHeaderView() {
        View view = main_nav_view.getHeaderView(0);
        TextView tv_header_title = view.findViewById(R.id.tv_header_title);
        tv_header_title.setText("Wake");
        TextView tv_header_content = view.findViewById(R.id.tv_header_content);
        tv_header_content.setText("Laravle/Android/React全栈开发");
    }

    private void initToolBar() {
        mToolBar = findViewById(R.id.mToolBar);
        setSupportActionBar(mToolBar);
    }

    private void initView() {

        main_drawer_layout = findViewById(R.id.main_drawer_layout);
        main_nav_view = findViewById(R.id.main_nav_view);
        // 设置监听
        main_nav_view.setNavigationItemSelectedListener(this);
        // 去掉侧边栏菜单渲染
        main_nav_view.setItemIconTintList(null);

        // 侧拉菜单方法
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,
                main_drawer_layout,
                mToolBar,
                R.string.navigation_drawer_open,
                R.string.navigation_drawer_close);

        // 切换菜单的动画
        main_drawer_layout.addDrawerListener(toggle);

        // 同步状态
        toggle.syncState();

        // 显示默认的 Fragment
        showHomeFragment();
    }

    /**
     * 当导航菜单被点击时
     * @param menuItem
     * @return 返回true表示开启点击事件， 返回 false 表示关闭菜单点击事件
     */
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.nav_menu_home:
                showHomeFragment();
                break;
            case R.id.nav_menu_wallpaper:
                showWallpaperFragment();
                break;
            case R.id.nav_menu_app:
                showAppFragment();
                break;
            case R.id.nav_menu_system:
                showSystemFragment();
                break;
//            case R.id.nav_menu_time:
//                showTimerFragment();
//                break;
            case R.id.nav_menu_share:
                // 分享Dialog
                showShareDialog();
                break;
            case R.id.nav_menu_setting:
                startActivity(new Intent(this, SettingActivity.class));
                break;
        }
        main_drawer_layout.closeDrawer(GravityCompat.START); // 点击选择后，自动收起菜单
        return true;
    }

    private void showShareDialog() {
        Dialog mShareDialog = new Dialog(this);
        View view = View.inflate(this, R.layout.layout_share_item, null);
        mShareDialog.setContentView(view);

        mShareDialog.show();
    }

    /**
     * 添加右侧按钮
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 右上角点击菜单
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_san:
                break;
            case R.id.menu_http:
                break;
            case R.id.menu_share:
                break;
            case R.id.menu_setting:
                Toast.makeText(this, "设置", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showHomeFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
            ft.add(R.id.main_content_layout, homeFragment);
        }
        hideAllFragment(ft);
        ft.show(homeFragment);
        ft.commit();
    }
    private void showWallpaperFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (wallpaperFragment == null) {
            wallpaperFragment = new WallpaperFragment();
            ft.add(R.id.main_content_layout, wallpaperFragment);
        }
        hideAllFragment(ft);
        ft.show(wallpaperFragment);
        ft.commit();
    }
    private void showAppFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (appFragment == null) {
            appFragment = new AppFragment();
            ft.add(R.id.main_content_layout, appFragment);
        }
        hideAllFragment(ft);
        ft.show(appFragment);
        ft.commit();
    }
    private void showSystemFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (systemFragment == null) {
            systemFragment = new SystemFragment();
            ft.add(R.id.main_content_layout, systemFragment);
        }
        hideAllFragment(ft);
        ft.show(systemFragment);
        ft.commit();
    }
    private void showTimerFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (timerFragment == null) {
            timerFragment = new TimerFragment();
            ft.add(R.id.main_content_layout, timerFragment);
        }
        hideAllFragment(ft);
        ft.show(timerFragment);
        ft.commit();
    }

    /**
     * 隐藏所有的 Fragment
     * @param fb
     */
    private void hideAllFragment(FragmentTransaction fb) {
        if (homeFragment != null) {
            fb.hide(homeFragment);
        }
        if (wallpaperFragment != null) {
            fb.hide(wallpaperFragment);
        }
        if (appFragment != null) {
            fb.hide(appFragment);
        }
        if (systemFragment != null) {
            fb.hide(systemFragment);
        }
        if (timerFragment != null) {
            fb.hide(timerFragment);
        }
    }

    /**
     * 记录Fragment
     * 此方法防止 Fragment 重叠
     * @param fragment
     */
    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
        if (homeFragment == null && fragment instanceof HomeFragment) { //当 homeFragment 对象为空，并且又指向 HomeFragment
            homeFragment = (HomeFragment) fragment;
        } else if (wallpaperFragment == null && fragment instanceof WallpaperFragment) {
            wallpaperFragment = (WallpaperFragment) fragment;
        } else if (appFragment == null && fragment instanceof AppFragment) {
            appFragment = (AppFragment) fragment;
        } else if (systemFragment == null && fragment instanceof SystemFragment) {
            systemFragment = (SystemFragment) fragment;
        } else if (timerFragment == null && fragment instanceof TimerFragment) {
            timerFragment = (TimerFragment) fragment;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(butteryReceiver);
    }

    class ButteryReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
                // 当前电量
                int level = intent.getIntExtra("level", 0);
                // 总电量
                int intScale = intent.getIntExtra("scale", 0);
                // 温度
                Constant.BUTTERY_TEMP = intent.getIntExtra("temperature", 0);

                Constant.BUTTERY_T = (level*100 / intScale) + "%";

            }
        }
    }
}
